package com.fuwo.spider.service;
import com.fuwo.spider.entity.Article;
import com.fuwo.spider.service.repository.ArticleRepository;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class SpiderServiceImpl implements SpiderService{
    private static final Logger log=LoggerFactory.getLogger(SpiderServiceImpl.class);
    private static final String defaultImgUrl="http://img-test.fuwo.com//admin/designer/201807/03/9c6f26b6-0bc1-466e-b04f-34bbd7daea66.png";
    @Autowired
    private ArticleRepository articleRepository;
    @Override
    public void spiderArticleImage(){
        //String testUrl="http://www.fuwo.com/article/4000009.html";
        List<Article> articles=articleRepository.findAll();
        long start=System.currentTimeMillis();
        int i=0;
        for (Article article : articles) {
            String imgUrl = getImageUrl(article.getUrl());
            if (imgUrl == null) {
                i = i + 1;
                imgUrl = defaultImgUrl;
            }
            article.setCover(imgUrl);
        }
        log.info("爬取用时："+(System.currentTimeMillis()-start)+"ms");
        log.info("总文章数："+articles.size());
        log.info("未找到封面图的文章数："+i);
        //这个方法可以自动更新数据，但它是根据id一个一个更新的，如果追求速度的话可以自己写个批量更新的代码
        articleRepository.saveAll(articles);
    }

    private String getImageUrl(String url){
        Document doc= null;
        //注意设置userAgent，伪装成浏览器
        try {
            doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36")
                    .get();
        } catch (IOException e) {
            log.error("获取页面元素失败，html链接："+url+" 原因："+e.getMessage());
            return null;
        }
        Element article=doc.getElementsByClass("article").first();
        if(article==null){
            log.info("文章元素为空，请检查网页："+url);
            return null;
        }
        Element imgElement=article.getElementsByTag("img").first();
        if(imgElement==null){
            log.info("此网页没有图片，请检查网页："+url);
            return null;
        }
        return imgElement.attr("src");
    }

}
